<div id="tb<?php echo $uniqid;?>" style="padding:5px">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:;" onclick="menusAdd();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-tianjia1"></i><span class="l-btn-text">增加</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:;" onclick="menusEdit();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-bianji"></i><span class="l-btn-text">编辑</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:;" onclick="menusDelete();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shanchu"></i><span class="l-btn-text">删除</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:;" onclick="menusReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
            <td>&nbsp;&nbsp;<input id="menusKeywords" style="width:100px" type="text" class="easyui-input textbox"></td>
            <td><a href="javascript:void(0);" onclick="menusSearch();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span class="l-btn-text">查询</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>

<div class="con" onselectstart="return false;" style="-moz-user-select:none;">
    <table id="menus"></table>
</div>
<div id="iconlistdlg" class="easyui-dialog" style="width:720px; height:420px;" title="信息框" closed="true"></div>
<script>
    var menusAdd = function () {
        let title= '前台菜单添加';
        let url = '/admin/membersmenus/add';
        $('#rightTabs').tabs('close', title);
        addTab(title, url);
    };
    var menusEdit = function () {
        var row = $('#menus').treegrid('getSelected');
        if (row) {
            let title= '前台菜单编辑';
            let url = '/admin/membersmenus/edit?id=' + row.id;
            $('#rightTabs').tabs('close', title);
            addTab(title, url);
        } else {
            $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
        }
    };
    var menusDelete = function () {
        var row = $('#menus').treegrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '确定删除吗？', function (r) {
                if (r == true) {
                    $.messager.progress();
                    $.post('/admin/membersmenus/delete', {id: row.id}, function (data) {
                        $.messager.progress('close');
                        if (data.ret == 0) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            menusReload();
                        } else {
                            $.messager.alert('提示', data.msg, 'warning');
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.alert('提示', '请先选择要删除的行！', 'warning');
        }
    };
    var menusReload = function () {
        $("#menus").treegrid('reload');
    };
    var menusStatus = function(id, status){
        $.post('/admin/membersmenus/status', {id: id, status:status}, function (data) {
            if (data.ret == 0) {
                $.messager.show({
                    title: '提示',
                    msg: data.msg,
                    timeout: 3000,
                    showType: 'slide'
                });
                menusReload();
            } else {
                $.messager.alert('提示', data.msg, 'warning');
            }
        }, 'json');
    };
    var menusSearch = function () {
        var height = $(window).height() - $(".top").height() - 46;
        var width = $(window).width() - $(".leftmenu").width() - 13;
        $("#menus").treegrid({
            title: '前台菜单',
            width: width,
            height: height,//高度
            singleSelect: true,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: true,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 10,
            pageList: [10, 15, 20, 25],
            method: 'post',
            sortName: 'sortorder',
            sortOrder: 'desc',
            idField: 'id',
            treeField: 'name',
            url: '/admin/membersmenus/get',
            queryParams: {keywords: $('#menuskeywords').val()},
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: true,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: true,
            checkOnSelect: true,
            toolbar: '#tb<?php echo $uniqid;?>',
            columns: [[
                {field: 'name', title: '菜单名', width: '10%'},
                {field: 'icon', title: '图标', width: '5%', align:'center', formatter:function(value, rowData, rowIndex) {
                        return '<i style="font-size: 18px;width:18px;height:18px;color: #FF3300;" class="iconfont '+ value + '"></i>';
                    }},
                {field: 'links', title: '链接', width: '20%'},
                {field: 'flinks', title: '前端链接', width: '20%'},
                {field: 'show_on_nav', title: '快捷导航', width: '10%', align:'left', formatter:function(value, rowData, rowIndex) {
                    if(rowData.up>0) {
                        if (value == 1) {
                            return '<div title="是" class="switch active" onClick="menusStatus(' + rowData.id + ', 0)"><div class="circle"></div></div>';
                        } else {
                            return '<div title="否" class="switch" onClick="menusStatus(' + rowData.id + ', 1)"><div class="circle"></div></div>';
                        }
                    }
                }},
                {field: 'sortorder', title: '排序', width: '5%'},
                {field: 'created_at', title: '创建时间', width: '20%'},
            ]],
            onDblClickRow: function (index, field) {
                menusEdit();
            },
            onLoadSuccess: function (row, data) {
                $.each(data, function (i, val) {
                    //$('#menus').treegrid('collapseAll', data[i].id)
                })
            }
        });
    };
    menusSearch();
</script>
